package main

import (
	"encoding/json"
	"fmt"
	"os"
)

type Config struct {
	DefaultLibraryName string
	Libraries          map[string]Library
}
type Library struct {
	Name string
	Path string
}

func LoadConfig() *Config {
	file, err := os.Open("config.json")
	if err != nil {
		// Handle error
		panic(err)
	}
	defer func(file *os.File) {
		err := file.Close()
		if err != nil {
			panic(err)
		}
	}(file)

	var config Config
	decoder := json.NewDecoder(file)
	_ = decoder.Decode(&config)
	fmt.Printf("%+v\n", config)
	return &config
}
func (c *Config) WriteConfig() {
	file, err := os.OpenFile("config.json", os.O_WRONLY, 666)
	if err != nil {
		// Handle error
	}
	defer func(file *os.File) {
		err := file.Close()
		if err != nil {

		}
	}(file)

	jsonData, err := json.Marshal(c)
	if err != nil {
		// Handle error
	}
	fmt.Println()
	_, err = file.Write(jsonData)
	if err != nil {
		// Handle error
		fmt.Println(err)
	}
}
